#include "Exception.h"
#include <cstdlib>
#include <cstring>

namespace DSLib
{

void Exception::init(const char* message, const char* file, int line)
{
    m_message = message ? _strdup(message) : NULL;
    if (file)
    {
        char sl[16] = {0};
        _itoa(line, sl, 10);
        m_location = static_cast<char*>(malloc(strlen(m_message) + strlen(sl) + 2));
        if (m_location)
        {
            m_location = strcpy(m_location, file);
            m_location = strcat(m_location, ":");
            m_location = strcat(m_location, sl);
        }
    }
    else
    {
        m_location = NULL;
    }
}

Exception::Exception(const char *message)
{
    init(message, NULL, 0);
}

Exception::Exception(const char *file, int line)
{
    init(NULL, file, line);
}

Exception::Exception(const char *message, const char *file, int line)
{
    init(message, file, line);
}

Exception::Exception(const Exception &e)
{
    m_message = _strdup(e.message());
    m_location = _strdup(e.location());
}

Exception& Exception::operator= (const Exception& e)
{
    if (this != &e)
    {
        free(m_message);
        free(m_location);
        m_message = _strdup(e.message());
        m_location = _strdup(e.location());
    }
    return *this;
}

const char* Exception::message() const
{
    return m_message;
}

const char* Exception::location() const
{
    return m_location;
}

Exception::~Exception()
{
    free(m_message);
    free(m_location);
}

}



